import 'package:crypto_x/crypto_x.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/models/city_entity.dart';
import 'package:flutter_deer/login/iview/reset_password_iview.dart';
import 'package:flutter_deer/login/models/register_user_entity.dart';
import 'package:flutter_deer/mvp/base_page_presenter.dart';
import 'package:flutter_deer/net/base_entity.dart';
import 'package:flutter_deer/net/net.dart';
import 'package:flutter_deer/res/constant.dart';

class ResetPasswordPagePresenter extends BasePagePresenter<ResetPasswordIMvpView> {

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
    });
  }

  Future<dynamic> sendResetPasswordSmsCode(String mobile) async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      requestNetwork<OperatorBaseEntity>(Method.get,
          url: HttpApi.resetPasswordSmsCode.replaceAll("{mobile}", mobile),
          onSuccess: (data) {},
          onError: (code, msg) {

          }
      );
    });
  }

  Future<dynamic> resetPassword(String mobile,String code,String password) async {
    // final key = CipherKey.fromUTF8(Constant.encryptPasswordKey);
    // final iv = CipherIV.fromUTF8(Constant.encryptPasswordKey);
    // var aes = AES(key: key, mode: AESMode.cfb,padding: null);
    // CryptoBytes encrypted = aes.encrypt(CryptoBytes.fromUTF8(password), iv: iv);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final RegisterUserEntity registerUserEntity = RegisterUserEntity(
          mobile: mobile.trim(), code: code.trim(), password: password.trim());
      requestNetwork<OperatorBaseEntity>(Method.put,
          url: HttpApi.resetPassword,
          params: registerUserEntity.toJson(),
          onSuccess: (data) {
            view.resetPasswordResult(data!.data);
          },
          onError: (code, msg) {

          }
      );
    });
  }

  void testListData() {
    /// 测试返回List类型数据解析
    asyncRequestNetwork<List<CityEntity>>(Method.get,
      url: HttpApi.subscriptions,
      onSuccess: (data) {

      },
        onError: (code,msg){

        }
    );
  }
}
